$(function () {
    var user = JSON.parse(sessionStorage.getItem('user'));
    console.log(user);
    if (user) {
        var userId = user.id;
        var html = '';
        $.get("/shopcars/" + userId, function (data) {
            console.log(data);
            if (data) {
                $.each(data, function (index, value) {
                    html += '<div class="product-box">\n' +
                        '            <div class="product-ckb"><em class="product-em product-xz"></em></div>\n' +
                        '            <div class="product-sx">\n' +
                        '                <a href="/mine/productDetails.html?id=' + value.product.id + '">\n' +
                        '                    <img src="' + value.product.proImgUrl + '" class="product-img" />\n' +
                        '                    <span class="product-name">' + value.product.proName + '</span>\n' +
                        '                </a>\n' +
                        '                <span class="product-price">㉿&thinsp;<span class="price">' + value.productPrice + '</span></span>\n' +
                        '                <div class="product-amount">\n' +
                        '                    <div class="product_gw">\n' +
                        '                        <em class="product-jian">-</em>\n' +
                        '                        <input type="text" value="1" class="product-num"/>\n' +
                        '                        <em class="product-add">+</em>\n' +
                        '                    </div>\n' +
                        '                </div>\n' +
                        '                <div class="product-del" id="' + value.product.id + '"><img src="/js/plugins/shopcarpl/img/deleteico.png"></div>\n' +
                        '            </div>\n' +
                        '        </div>';
                })
                $('.product').append(html);
                //加的效果
                $(".product-add").click(function () {
                    var n = $(this).prev().val();
                    var num = parseInt(n) + 1;
                    if (num == 99) {
                        return;
                    }
                    $(this).prev().val(num);
                    TotalPrice();
                });
                //减的效果
                $(".product-jian").click(function () {
                    var n = $(this).next().val();
                    var num = parseInt(n) - 1;
                    if (num == 0) {
                        return;
                    }
                    $(this).next().val(num);
                    TotalPrice();
                });

                $(".product-ckb").click(function () {
                    $(this).children("em").toggleClass("product-xz");
                    TotalPrice();
                    productxz();
                });
                //删除产品
                $(".product-del").click(function () {
                    var id = $(this)[0].id;
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '确定要移除此商品么',
                        onClickConfirmBtn: function () {
                            $.ajax({
                                type: "DELETE",
                                url: "/shopcars/" + id,
                                success: function (ele) {
                                    console.log(ele);
                                    if (ele.success) {
                                        window.location.reload();
                                    } else {
                                        $(document).dialog({
                                            titleShow: false,
                                            content: '您的网络不好，请稍后再试',
                                        });
                                    }
                                }
                            });
                        }
                    });

                });

                TotalPrice();
                shuliang();

                //结算
                $('.product-sett').click(function () {
                    var list = [];
                    var jifen = $('.all-price').html();//积分
                    $.get("/users/getScore/" + userId, function (data) {
                        if (jifen > data) {
                            $(document).dialog({
                                titleShow: false,
                                content: '你买不起',
                            });
                            return;
                        } else {
                            var $product = $('.product .product-box');
                            $product.each(function (index, value) {//数据
                                var pdanjia = parseInt(value.querySelector('.price').innerHTML);
                                var pnums = parseInt(value.querySelector('.product-num').value);
                                var pid = parseInt(value.querySelector('.product-del').id);
                                var pname = value.querySelector('.product-name').innerHTML;
                                var obj = {"pid": pid, "pname": pname, "pprice": pdanjia, "pnums": pnums, "uid": userId}
                                list.push(obj)
                            })
                            console.log(list);
                            var s = JSON.stringify(list);
                            $.post("/forms/users", {s: s}, function (data) {
                                console.log(data);
                                if (data.success) {
                                    $(document).dialog({
                                        type: 'confirm',
                                        closeBtnShow: true,
                                        content: '是否要跳到订单页面',
                                        onClickConfirmBtn: function () {
                                            window.location.href = "/mine/order.html?uuid=" + data.data;
                                        }, onClickCancelBtn: function () {
                                            window.location.reload();
                                        }
                                    });
                                } else {
                                    $(document).dialog({
                                        titleShow: false,
                                        content: '网络延迟....',
                                    });
                                }

                            })

                        }
                    })

                });

                //选中产品
                function productxz() {
                    var xz = $(".product-em");
                    var xz1 = $(".product-xz");
                    if (xz1.length == xz.length) {
                        $(".product-all em").addClass("product-all-on");
                    } else {
                        $(".product-all em").removeClass("product-all-on");
                    }
                    shuliang();
                    TotalPrice();

                }

//计算产品价格
                function TotalPrice() {
                    //总价
                    var total = 0;
                    $(".product-em").each(function () {

                        if ($(this).is(".product-xz")) {
                            var price = parseInt($(this).parents(".product-ckb").siblings().find(".price").text());//得到产品单价
                            var slproice = parseInt($(this).parents(".product-ckb").siblings().find(".product-num").val());//得到产品数量
                            var dgtotal = price * slproice;
                            total += dgtotal;
                        }
                        $(".all-price").text(total.toFixed(2)); //输出全部总价
                    });

                }

//获取选择产品数量
                function shuliang() {
                    $(".product-all-sl").text("");
                    var cd = $(".product-xz").length;
                    $(".product-all-sl").text(cd);
                    if (cd > 0) {
                        $(".product-all-qx").text("已选");
                        $(".all-sl").css("display", "inline-block");
                        $(".product-sett").removeClass("product-sett-a");
                    } else {
                        $(".product-all-qx").text("全选");
                        $(".all-sl").css("display", "none");
                        $(".product-sett").addClass("product-sett-a");
                    }
                }

            }
        })
    }

})
